package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.RoomInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
  @Autowired
  RoomInfoMapper roomInfoMapper;
  @Autowired
  GraphInfoService graphInfoService;
  @Autowired
  AttrValueService attrValueService;
  @Autowired
    FacilityInfoService facilityInfoService;
  @Autowired
    LabelInfoService labelInfoService;
  @Autowired
   PaymentTypeService paymentTypeService;
  @Autowired
  LeaseTermService leaseTermService;
  @Autowired
  RoomAttrValueService roomAttrValueService;
  @Autowired
  RoomFacilityService roomFacilityService;
  @Autowired
  RoomLabelService roomLabelService;
  @Autowired
  RoomPaymentTypeService roomPaymentTypeService;
  @Autowired
  RoomLeaseTermService roomLeaseTermService;
    @Override
    public IPage<RoomItemVo> getRoomItemVo(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.getRoomItemVo(page,queryVo);
    }

  @Override
  public RoomDetailVo getRoomDetailVoById(Long id) {
    RoomDetailVo roomDetailVoById = roomInfoMapper.getRoomDetailVoById(id);
     List<GraphVo> graphVoList=graphInfoService.listGraphVoByRoomId(id);
     List<AttrValueVo> attrValueVo=attrValueService.getRoomAttrvalueVo(id);
     List<FacilityInfo> facilityInfoList=facilityInfoService.getRoomFacilityById(id);
    List<LabelInfo> labelInfoList=labelInfoService.getLabelsByRoomId(id);
    List<PaymentType> paymentTypes=paymentTypeService.getPaymentTypeByRoomId(id);
   List<LeaseTerm> leaseTermList=leaseTermService.getLeaseTermsByRoomId(id);

   roomDetailVoById.setLeaseTermList(leaseTermList);
    roomDetailVoById.setPaymentTypeList(paymentTypes);
    roomDetailVoById.setLabelInfoList(labelInfoList);
     roomDetailVoById.setFacilityInfoList(facilityInfoList);
     roomDetailVoById.setGraphVoList(graphVoList);
     roomDetailVoById.setAttrValueVoList(attrValueVo);
    return roomDetailVoById;
  }

    @Override
    public void saveOrUpdateRoomSubmitVo(RoomSubmitVo roomSubmitVo) {
        Long id = roomSubmitVo.getId();

        this.saveOrUpdate(roomSubmitVo);
        if(id!=null)
        {
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId,id).eq(GraphInfo::getItemType,2));
            roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId,id));
            roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId,id));
            roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId,id));
            roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId,id));
            roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId,id));
        }
        Long newId = roomSubmitVo.getId();
        ArrayList<GraphInfo> graphInfos = new ArrayList<>();
         List<GraphVo> graphVoList=roomSubmitVo.getGraphVoList();
         graphVoList.forEach(g->{
             GraphInfo graphInfo = new GraphInfo();
             graphInfo.setUrl(g.getUrl());
             graphInfo.setName(g.getName());
             graphInfo.setItemType(ItemType.ROOM);
             graphInfo.setItemId(newId);
             graphInfos.add(graphInfo);
         });
        graphInfoService.saveBatch(graphInfos);


        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        List<RoomAttrValue> roomAttrValues=new ArrayList<>();
        attrValueIds.forEach(
                ids->{
                    RoomAttrValue build = RoomAttrValue.builder().attrValueId(ids).roomId(newId).attrValueId(ids).build();
                    roomAttrValues.add(build);
                }
        );
        roomAttrValueService.saveBatch(roomAttrValues);

        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        ArrayList<RoomFacility> roomFacilities = new ArrayList<>();
        facilityInfoIds.forEach(ids->{
            RoomFacility build = RoomFacility.builder().facilityId(ids).roomId(newId).build();
            roomFacilities.add(build);
        });
        roomFacilityService.saveBatch(roomFacilities);

        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        ArrayList<RoomLabel> roomLabels=new ArrayList<>();
        labelInfoIds.forEach(ids->{
            RoomLabel build = RoomLabel.builder().roomId(newId).labelId(ids).build();
            roomLabels.add(build);
        });
  roomLabelService.saveBatch(roomLabels);

        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        List<RoomPaymentType> roomPaymentTypes=new ArrayList<>();
        paymentTypeIds.forEach(ids->{
            RoomPaymentType build = RoomPaymentType.builder().roomId(newId).paymentTypeId(ids).build();
            roomPaymentTypes.add(build);
        });
         roomPaymentTypeService.saveBatch(roomPaymentTypes);

        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
         List<RoomLeaseTerm> roomLeaseTerms=new ArrayList<>();
         leaseTermIds.forEach(ids->{
             RoomLeaseTerm build = RoomLeaseTerm.builder().leaseTermId(ids).roomId(newId).build();
             roomLeaseTerms.add(build);
         });
         roomLeaseTermService.saveBatch(roomLeaseTerms);
    }

    @Override
    public void removeRoomById(Long id) {
        graphInfoService.removeById(id);
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId,id).eq(GraphInfo::getItemType,2));
        roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId,id));
        roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId,id));
        roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId,id));
        roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId,id));
        roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId,id));
    }
}




